#include <bits/stdc++.h>
using namespace std;

int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

void write(int x)
{
    if (x < 0)
    {
        putchar('-');
        write(-x);
        return;
    }
    if (x / 10)
        write(x / 10);
    putchar(x % 10 + '0');
}

const int N = 2e3 + 5;
int T, n, a[N], dp[N][N], sum[N];

int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    T = read();
    while (T--)
    {
        n = read();
        for (int i = 1; i <= n; i++)
            a[i] = read();
        for (int i = 1; i <= n; i++)
        {
            sum[i] = sum[i - 1];
            if (a[i] == a[i - 1])
                sum[i] += a[i];
        }
        // cin >> n;
        // for (int i = 1; i <= n; i++)
        //     cin >> a[i];
        memset(dp, 0, sizeof dp);
        for (int i = 1; i <= n; i++)
        {
            dp[i][0] = sum[i];
            for (int j = 1; j < i; j++)
            {
                dp[i][j] = max(dp[i][j], sum[j] + sum[i] - sum[j + 1]);
                for (int k = 1; k < j; k++)
                {
                    if (a[k] == a[j + 1])
                        dp[i][j] = max(dp[i][j], dp[j][k] + a[k] + sum[i] - sum[j + 1]);
                    else
                        dp[i][j] = max(dp[i][j], dp[j][k] + sum[i] - sum[j + 1]);
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            // if (dp[n][i] > ans)
            //     ans = dp[n][i], id = i;
            ans = max(ans, dp[n][i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}